# Maintainer: Daniel Bermond <dbermond@archlinux.org>
# Maintainer: Bruno Pagani <archange@archlinux.org>

_srcname=SPIRV-LLVM-Translator
pkgname=${_srcname,,}
pkgver=16.0.0
pkgrel=1
pkgdesc="Tool and a library for bi-directional translation between SPIR-V and LLVM IR"
arch=(loong64 x86_64)
url="https://github.com/KhronosGroup/SPIRV-LLVM-Translator"
license=(custom)
depends=(llvm-libs spirv-tools)
makedepends=(git cmake llvm spirv-headers)
makedepends+=(python python-setuptools clang)
# Current point in the used LLVM branch
#_commit=81c9ac632ead5c233728725179ed2530c2c820ec
_commit=093814af9ac10647c66f94f5332d328166d70928
source=(git+${url}.git#commit=$_commit
  spirv-llvm-translator-16.0.0-ld_library_path.patch
  spirv-llvm-translator-16.0.0-llvm-link-llvm-dylib.patch)
sha256sums=('SKIP'
            'a36ce0c02bf3939199966ca049c2269d2c00e753ff906f74a2ca8e3712c24602'
            '62d9b1d1246918fdafea198bd3e80d80b05b0ab0e4c09c5bf39a57867dac9a1a')

pkgver() {
  cd ${_srcname}
  echo "16.0.0"
#  git describe --tags | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./;s/-/+/'
#dev.build+r534-g093814af
}

prepare() {
  cd ${_srcname}
  patch -p1 -i $srcdir/spirv-llvm-translator-16.0.0-ld_library_path.patch
  patch -p1 -i $srcdir/spirv-llvm-translator-16.0.0-llvm-link-llvm-dylib.patch
}

build() {
  cmake -B build -S ${_srcname} \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
    -DCMAKE_SKIP_RPATH=ON \
    -DLLVM_INCLUDE_TESTS=ON \
    -DLLVM_EXTERNAL_LIT=/usr/bin/lit \
    -DLLVM_EXTERNAL_SPIRV_HEADERS_SOURCE_DIR=/usr/include/spirv/ \
    -DBASE_LLVM_VERSION=16.0.0 \
    -Wno-dev
  make -C build
}

check() {
  LD_LIBRARY_PATH="${srcdir}/build/lib/SPIRV" make -C build test
}

package() {
  make -C build DESTDIR="${pkgdir}" install
  install -Dm755 build/tools/llvm-spirv/llvm-spirv -t "${pkgdir}"/usr/bin
  install -Dm644 ${_srcname}/LICENSE.TXT -t "${pkgdir}"/usr/share/licenses/${pkgname}/
}
